﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class AddProduct : System.Web.UI.Page
    {
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                CategoryController categoryController=new CategoryController();
                IList<CategoryEntity> categoryEntities = categoryController.FindAllCategory();
                CategoryDropDownList.DataSource = categoryEntities;
                CategoryDropDownList.DataTextField = "Name";
                CategoryDropDownList.DataValueField = "Id";
                CategoryDropDownList.DataBind();
                TypeDropDownList.DataSource = new ListItemCollection()
                                             {
                                                 new ListItem("Part","1"),
                                                 new ListItem("Assembly","2"),
                                             };
                TypeDropDownList.DataBind();


            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            ProductController productController=new ProductController();
            ProductEntity productEntity=new ProductEntity();
            productEntity.No = ProductNoTextBox.Text.Trim();
            productEntity.Name = ProductNameTextBox.Text.Trim();
            productEntity.Remark = RemarkTextBox.Text.Trim();
            productEntity.Revision = RevTextBox.Text.Trim();
            productEntity.Description = DescTextBox.Text.Trim();
            productEntity.CategoryId = Convert.ToInt32(CategoryDropDownList.SelectedValue);
            productEntity.Type = CategoryDropDownList.SelectedValue;
            productEntity.Visibility = VisibilityCheckBox.Checked;
            productController.AddProduct(productEntity);
            Session["FlashMessage"] = "Success: Add Product Successful!";
            Session["FlashMessageType"] = "success";

        }
    }
}